<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <script src="./static/bootstrap/js/jquery-1.10.2.min.js"></script>
    <script src="./static/bootstrap/js/jquery.md5.js"></script>
    <link rel="stylesheet" href="./static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./static/bootstrap/css/signin_yj.css">
    <script src="../static/js/md5.js"></script>
</head>
<body>
<div class="container">
    <form class="form-signin" style="text-align: center;">
        <h2 class="form-signin-heading">表单上传登录</h2>
        <br>
        <div class="input-group">
	        <span class="input-group-addon">
	            <a class="glyphicon glyphicon-user"></a>
	        </span>
            <input class="form-control" type="text" id="username"  placeholder='用户名' autofocus>
        </div>
        <br>
        <div class="input-group" >
	        <span class="input-group-addon">
	            <a class="glyphicon glyphicon-lock"></a>
	        </span>
            <input class="form-control" type="password" id="password"  placeholder='密码' onkeydown='KeyDown()'>
        </div>
        <br>
        <div class="input-group">
	        <span class="input-group-addon">
	        	<a class="glyphicon glyphicon-check"></a>
	        </span>
            <input class="form-control  " type="text" id="certcode" placeholder="输入验证码" onkeydown='KeyDown()'>
            <span class="input-group-addon">
                <img src="./getVerfyImage"  onclick="reloadimage();" id="safeCode"  style="cursor:pointer">
            </span>
        </div>
        <br>
        <button class="btn btn-lg btn-primary btn-block" type="button" onclick="checklogin()" >登录</button>
    </form>
</div> <!-- /container -->
<script>


    function KeyDown(){
        if (event.keyCode == 13){
            event.returnValue=false;
            event.cancel = true;
            checklogin();
        }
    }

    function reloadimage() {
        $("#safeCode").attr("src","./getVerfyImage?date=" + new Date());
    }

    function checklogin(){
        var username = $("#username").val();
        var password = $("#password").val();
        var verifyCode = $("#certcode").val();
        if ( username.length==0 || password.length==0){
            alert("用户名或密码不能为空");
            return false;
        }
        var now = new Date().getTime();
        var vstr = hex_md5(password);
        var vkey = hex_md5(username+vstr+verifyCode+now);

        console.log(vstr);
        console.log(vkey);

        $.ajax({
            type: "post",
            url: "./loginVerify",
            dataType: "json",
            data: {
                "action":"login_check",
                "username":username,
                "password":vstr,
                "verifyCode":verifyCode,
                "timeStamp": now,
                "token": vkey
            },
            async: true,
            //异步请求（true）,默认是异步，同步是false
            error: function()
            {
                alert("系统错误，请联系管理员！");
            },
            success: function(data)
            {
                //alert(data.flag);
                if (data.code == 200){
                	window.location.href="./customize_form";
                } else if (data.code == -1 || data.code == -2) {
                    alert(data.msg);
                    $("#safeCode").attr("src","./getVerfyImage?date=" + new Date());
                } else {
                    alert(data.msg);
                    window.location.href="./login";
                }
            }
        });
    }

</script>
</body>
</html>